package tq.com.systools.system;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PrivilageHelper {
    static public boolean checkWritePermition(Context context){
        return ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED ;
    }

    static public boolean checkAudioPermition(Context context){
        return ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED ;
    }

    static public  PermitionCheck requestPermition(Activity activity, String[] permition, int requestCode){
        PermitionCheck permitionCheck = new PrivilageHelper.PermitionCheck(permition, requestCode);
        activity.requestPermissions(permition, requestCode);
        return permitionCheck;
    }

    static public class PermitionCheck{
        private int requestCode ;
//        private String [] permition;
        private List<String> permition ;

        public PermitionCheck(String [] request, int code){
            requestCode = code;
            permition = Arrays.asList(request);
        }
        public int getRequestCode(){
            return requestCode;
        }
        public void checkResult(String[] permition, int[] result, PermitionResult callback){
            for (int i = 0; i < permition.length; i++){
                String item = permition[i];

                if(!this.permition.contains(item)){
                    continue;
                }
                if(callback != null){
                    if(result[i] == PackageManager.PERMISSION_GRANTED){
                        callback.success(item);
                    }else{
                        callback.failed(item);
                    }
                }
            }
        }


    }

    public interface PermitionResult{
        void success(String permition);
        void failed(String permition);
    }
}
